﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class DC_CommandCommittee : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private object _dataItem = null;

        private IMasRecordStatusBLL staComBLL = new MasRecordStatusBLL();
        private IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IJobSiteLevelSystemControlBLL jslscBLL = new JobSiteLevelSystemControlBLL();
        private CommandBLL cmdBLL = new CommandBLL();

        private IJobSiteTypeBLL jobTypeBLL = new JobSiteTypeBLL();
        Main main = new Main();

        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                bindStaCom();
                bindEmpType();
                FillDataToApprover();
                ListInvestType();
                DataSet ds = new DataSet();
                ds = jslscBLL.ListJobSiteLevelSystemControl();
                if (ds.Tables[0].Rows[0][0].ToString() == "1") //กระทรวง
                {
                    rdoFlag.Items[0].Enabled = false;
                    rdoFlag.Items[1].Selected = true;
                    rdoApptypeOut.Enabled = false;
                    txtAppName.Enabled = false;
                }
                else
                {
                    rdoFlag.Items[2].Enabled = false;
                    rdoFlag.Items[0].Selected = true;
                    rdoApptypeOut.Enabled = true;
                    txtAppName.Enabled = true;
                }
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblID.Text = DataBinder.Eval(DataItem, "cmd_seq").ToString();
                    ddlStatus.SelectedValue = DataBinder.Eval(DataItem, "cmd_status").ToString();
                    string strFlag = DataBinder.Eval(DataItem, "cmd_flag").ToString();
                    if (strFlag == "1")
                        rdoFlag.Items[0].Selected = true;
                    if (strFlag == "2")
                        rdoFlag.Items[1].Selected = true;
                    if (strFlag == "3")
                        rdoFlag.Items[2].Selected = true;

                    txtDraftNo.Text = DataBinder.Eval(DataItem, "cmd_draft_no").ToString();
                    txtRefNo.Text = DataBinder.Eval(DataItem, "cmd_ref_no").ToString();
                    txtName.Text = DataBinder.Eval(DataItem, "cmd_name").ToString();
                    txtAppDate.SetValue(DataBinder.Eval(DataItem, "approved_date", "{0:dd/MM/yyyy}").ToString());
                    ddlEmployeeType.SelectedValue = DataBinder.Eval(DataItem, "emp_type_seq").ToString();

                    string AppID = DataBinder.Eval(DataItem, "approved_seq").ToString();
                    if (AppID != "")
                    {
                        cmbEmployee.SelectedValue = AppID;
                        rdoApptypeIn.Checked = true;
                        rdoApptypeOut.Checked = false;
                        txtAppName.Enabled = false;
                    }
                    else
                    {
                        txtAppName.Text = DataBinder.Eval(DataItem, "approved_name").ToString();
                        rdoApptypeIn.Checked = false;
                        rdoApptypeOut.Checked = true;
                        txtAppName.Enabled = true;

                        cmbEmployee.Enabled = false;
                        cmbEmployee.ClearSelection();
                        cmbEmployee.Text = string.Empty;
                        cmbEmployee.Text = "... เลือกผู้อนุมัติ ...";
                    }
                    txtAppPosition.Text = DataBinder.Eval(DataItem, "approved_positions").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                    cmbInvestType.SelectedValue = DataBinder.Eval(DataItem, "inv_type_seq").ToString();
                    rdoStatus.SelectedValue = DataBinder.Eval(DataItem, "confirm").ToString();
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void bindStaCom()
        {
            IList<MasRecordStatusInfo> ListStatusCommand = staComBLL.ListStatusCommand();
            ddlStatus.DataSource = ListStatusCommand;
            ddlStatus.DataBind();
        }
        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes(1);
            ddlEmployeeType.DataSource = infoList;
            ddlEmployeeType.DataBind();
        }

        public void FillDataToApprover()
        {
            DataSet ds = empBLL.LoadEmployeeForApproveCommands();
            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = dt.Rows[i]["EMP_NAME"].ToString();
                item.Value = dt.Rows[i]["EMP_SEQ"].ToString();

                item.Attributes.Add("EMP_NAME", dt.Rows[i]["EMP_NAME"].ToString());
                item.Attributes.Add("EXEC_POS_NAME", dt.Rows[i]["EXEC_POS_NAME"].ToString());
                item.Attributes.Add("CLASS_NAME", dt.Rows[i]["CLASS_NAME"].ToString());

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtAppDate.GetStringValue() == "")
                {
                    main.MessageAlert(Page, "กรุณาระบุวันที่อนุมัติ!!");
                    //Utils.Web.ShowAlert(this.Page, "กรุณาระบุวันที่อนุมัติ!!");
                    return;
                }

                reqcmbEmployee.Enabled = rdoApptypeIn.Checked;

                cmdBLL.InsertCmdCommittee(rdoFlag.SelectedValue
                    , txtDraftNo.Text.Trim()
                    , txtName.Text.Trim()
                    , txtRefNo.Text.Trim()
                    , 8888 //cmd_type_seq
                    , Int32.Parse(ddlEmployeeType.SelectedValue)
                    , ddlStatus.SelectedValue
                    , rdoApptypeIn.Checked ? Int32.Parse(cmbEmployee.SelectedValue) : (int?)null
                    , rdoApptypeIn.Checked ? cmbEmployee.SelectedItem.Text : txtAppPosition.Text.Trim()
                    , txtAppPosition.Text.Trim()
                    , txtAppDate.GetStringValue().Length > 0 ? DateTime.Parse(txtAppDate.GetStringValue()) : (DateTime?)null
                    , Int32.Parse(rdoStatus.SelectedValue)
                    , Int32.Parse(cmbInvestType.SelectedValue)
                    , txtRemarks.Text.Trim()
                    , ssMng.EmpSeq);
            }
            catch (Exception ex)
            {

            }
        }

        private void ListInvestType()
        {
            InvestigationTypeBLL invtBLL = new InvestigationTypeBLL();

            DataTable dt;

            dt = invtBLL.ListInvest();
         
            cmbInvestType.DataSource = dt;
            cmbInvestType.DataBind();
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtAppDate.GetStringValue() == "")
                {
                    main.MessageAlert(Page, "กรุณาระบุวันที่อนุมัติ!!");
                    return;
                }
               
                cmdBLL.UpdateCmdCommittee(Convert.ToInt32(lblID.Text.Trim())
                    , rdoFlag.SelectedValue
                    , txtDraftNo.Text.Trim()
                    , txtName.Text.Trim()
                    , txtRefNo.Text.Trim()
                    , 8888 //cmd_type_seq
                    , Int32.Parse(ddlEmployeeType.SelectedValue)
                    , ddlStatus.SelectedValue
                    , rdoApptypeIn.Checked ? Int32.Parse(cmbEmployee.SelectedValue) : (int?)null
                    , rdoApptypeIn.Checked ? cmbEmployee.SelectedItem.Text : txtAppName.Text.Trim()
                    , txtAppPosition.Text.Trim()
                    , txtAppDate.GetStringValue().Length > 0 ? DateTime.Parse(txtAppDate.GetStringValue()) : (DateTime?)null
                    , Int32.Parse(rdoStatus.SelectedValue)
                    , Int32.Parse(cmbInvestType.SelectedValue)
                    , txtRemarks.Text.Trim()
                    , ssMng.EmpSeq);

            }
            catch (Exception ex)
            {
                
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = false;
            cmbEmployee.Enabled = true;
            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
            txtAppName.Text = "";
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = true;
            cmbEmployee.Enabled = false;

            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
            cmbEmployee.Text = "...เลือกผู้อนุมัติ...";
        }
    }
}